/*
 * @Author: 'zhangkeqq' '2211709169@qq.com'
 * @Date: 2025-07-24 13:04:49
 * @LastEditors: 'zhangkeqq' '2211709169@qq.com'
 * @LastEditTime: 2025-08-04 16:48:38
 * @FilePath: \node-express\router_handle\index.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
const db = require('../db/index')
const bcrypt  = require('bcryptjs')
//导入jwt,用于生成token
const jwt = require('jsonwebtoken')
//导入jwt配置文件，用户加密跟解密
const jwtconfig = require('../jwt-config/index')
exports.register = (req,res)=>{
    //req是前端传过来的数据，也就是reques,res是返回给前端的数据，也就是result
    const reginfo = req.body
    //第一步，判断前端传过来的数据有没有空
    if(!reginfo.account || !reginfo.password){
        return res.send({
            status:1,
            msssage:'账号或者密码不能为空'
        })
    }
    //第二步，判断前端传过来的账号有没有已经在数据表中存在
    //需要使用mysql的select语句
    const sql = 'select * from users where account = ?'
    //第一个参数是执行语句，第二个是参数，第三个是一个函数，用于处理结果
    db.query(sql,reginfo.account,(err,results)=>{
        if(results.length > 0){
            return res.send({
                status:1,
                message:'账号已存在'
            })
        }
        //第三步，对密码进行加密
        // 需要使用加密中间件bcrypt.js
        // bcrypt.hashSyncd第一个参数是传入的密码，第二个参数是加密后的长度
        reginfo.password = bcrypt.hashSync(reginfo.password,10)
        //第四步，把账号跟密码插入到users表里面
        const sql1 = 'insert into users set ?'
        //注册身份
        const identity = '用户'
        //创建时间
        const create_time = new Date()
        db.query(sql1,{
            account:reginfo.account,
            password:reginfo.password,
            //身份
            identity,
            //创建时间
            create_time,
            //初始化未冻结状态为0
            status:0,
        },(err,results)=>{          
             // 第一个,插入失败
            // affectedRows为影响的行数，如果插入失败，那么就没有影响到行数，也就是行数不为1
            if(results.affectedRows !== 1){
                return res.send({
                    status:1,
                    message:'注册账号失败'
                })
            }
            res.send({
                status:0,
                message:'注册账号成功'
            })
        })
    })
}

exports.login = (req,res)=>{
    const loginfo = req.body
    //第一步 查看数据表中有没有前端传过来的账号
    const sql = 'select * from users where account = ?'
    db.query(sql,loginfo.account,(err,results)=>{
        //执行sql语句失败的情况，异步在数据库断开的情况下会执行
        if(err) return res.cc(err)
        if(results.length !== 1)  return res.cc('登录失败')
        //第二步，对前端传过滤的密码进行解密
        const comparseResult = bcrypt.compareSync(loginfo.password,results[0].password)
        if(!comparseResult){
            return res.cc('登录失败')
        }
        //第三步 对账号是否冻结做判断
        if(results[0].status == 1){
            return res.cc('账号被冻结')
        }
        //第四步，生成返回前端的token
        //剔除加密后的密码，头像，创建时间，更新时间
        const user = {
            ...results[0],
            password:'',
            imageUrl:'',
            create_time:'',
            update_time:'',
        }
        //设置token的有效时长，有效期为7个小时
        const tokenStr = jwt.sign(user,jwtconfig.jwtSecretKey,{
            expiresIn:'7h'
        })
        res.send({
            results:results[0],
            status:0,
            message:'登录成功',
            token:'Bearer '+ tokenStr
        })
    })
}

// 超级管理员路由
const superAdminRouter = [
  {
    name: "set",
    path: "/set",
    icon: "setting",
    meta: { title: "设置" },
    component: "set/index",
  },
  {
    name: "overview",
    path: "/overview",
    icon: "Grid",
    meta: { title: "系统概览" },
    component: "overview/index",
  },
  {
    name: "users",
    path:"/users",
    icon: "User",
    meta: {
      title: "用户管理",
    },
    children: [
      {
        name: "product_manage",
        path: "/users/product_manage",
        meta: { title: "产品管理员" },
        component: "user_manage/product_manage/index",
      },
      {
        name: "message_manage",
        path: "/users/message_manage",
        meta: { title: "消息管理员" },
        component: "user_manage/message_manage/index",
      },
      {
        name: "user_list",
        path: "/users/user_list",
        meta: { title: "用户列表" },
        component: "user_manage/user_list/index",
      },
      {
        name: "users_manage",
        path: "/users/users_manage",
        meta: { title: "用户管理" },
        component: "user_manage/users_manage/index",
      },
    ],
  },
  {
    name: "product",
    path:"/product",
    icon: "Goods",
    meta: { title: "产品管理" },
    children: [
      {
        name: "product_manage_list",
        path: "/product/product_manage_list",
        meta: { title: "产品列表" },
        component: "product/product_manage_list/index",
      },
      {
        name: "out_product_manage_list",
        path: "/product/out_product_manage_list",
        meta: { title: "出库列表" },
        component: "product/out_product_manage_list/index",
      },
    ],
  },
  {
    name: "message",
    path:'/message',
    icon: "ChatDotRound",
    meta: {
      title: "消息管理",
    },
    children: [
      {
        name: "message_list",
        path: "/message/message_list",
        meta: { title: "消息列表" },
        component: "message/message_list/index",
      },
      {
        name: "recycle",
        path: "/message/recycle",
        meta: { title: "回收站" },
        component: "message/recycle/index",
      },
    ],
  },

  {
    name: "file",
    path: "/file",
    icon: "Folder",
    meta: { title: "文件管理" },
    component: "file/index",
  },
  {
    name: "operation_log",
    path: "/operation_log",
    icon: "pointer",
    meta: { title: "操作日志" },
    component: "operation_log/index",
  },
  {
    name: "login_log",
    path: "/login_log",
    icon: "Tools",
    meta: { title: "登录日志" },
    component: "login_log/index",
  },
];

// 用户管理员路由
const userAdminRouter = [
  {
    name: "set",
    path: "/set",
    icon: "setting",
    meta: { title: "设置" },
    component: "set/index",
  },
  {
    name: "users",
    path: "/users",
    icon: "User",
    meta: {
      title: "用户管理",
    },
    children: [
      {
        name: "user_list",
        path: "/users/user_list",
        meta: { title: "用户列表" },
        component: "user_manage/user_list/index",
      },
      {
        name: "users_manage",
        path: "/users/users_manage",
        meta: { title: "用户管理" },
        component: "user_manage/users_manage/index",
      },
    ],
  },
  {
    name: "file",
    path: "/file",
    meta: { title: "文件管理" },
    component: "file/index",
  },
];
// 产品管理员路由
const productAdminRouter = [
  {
    name: "set",
    path: "/set",
    meta: { title: "设置" },
    component: "set/index",
  },
  {
    path: "/product",
    name: "product",
    icon: "Goods",
    meta: { title: "产品管理" },
    children: [
      {
        name: "product_manage_list",
        path: "/product/product_manage_list",
        meta: { title: "产品列表" },
        component: "product/product_manage_list/index",
      },
      {
        name: "out_product_manage_list",
        path: "/product/out_product_manage_list",
        meta: { title: "出库列表" },
        component: "product/out_product_manage_list/index",
      },
    ],
  },
  {
    name: "file",
    path: "/file",
    icon: "Folder",
    meta: { title: "文件管理" },
    component: "file/index",
  },
];
// 消息管理员路由
const messageAdminRouter = [
  {
    name: "set",
    path: "/set",
    icon: "setting",
    meta: { title: "设置" },
    component: "set/index",
  },
  {
    path:'/message',
    name: "message",
    icon: "ChatDotRound",
    meta: {
      title: "消息管理",
    },
    children: [
      {
        name: "message_list",
        path: "/message/message_list",
        meta: { title: "消息列表" },
        component: "message/message_list/index",
      },
        {
    name: "recycle",
    path: "/message/recycle",
    meta: { title: "回收站" },
    component: "message/recycle/index",
  },
    ],
  },


  {
    name: "file",
    path: "/file",
    icon:'Folder',
    meta: { title: "文件管理" },
    component: "file/index",
  },
];
// 普通用户路由
const userRouter = [
  {
    name: "set",
    path: "/set",
    icon: "setting",
    meta: { title: "设置" },
    component: "set/index",
  },
  {
    path:'/product',
    name: "product",
    icon: "Goods",
    meta: { title: "产品管理" },
    children: [
      {
        name: "product_manage_list",
        path: "/product/product_manage_list",
        meta: { title: "产品列表" },
        component: "product/product_manage_list/index",
      },
      {
        name: "out_product_manage_list",
        path: "/product/out_product_manage_list",
        meta: { title: "出库列表" },
        component: "product/out_product_manage_list/index",
      },
    ],
  },
  {
    name: "file",
    path: "/file",
    icon: "Folder",
    meta: { title: "文件管理" },
    component: "file/index",
  },
];

// 返回用户的路由列表，参数ID
exports.returnMenuList = (req,res) =>{
    const sql = 'select identity from users where id = ?'
    db.query(sql,req.body.id,(err,result)=>{
        if (err) return res.cc(err)
        let menu = []
        if(result[0].identity=='超级管理员'){
            menu = superAdminRouter
        }
        if(result[0].identity=='用户管理员'){
            menu = userAdminRouter
        }
        if(result[0].identity=='产品管理员'){
            menu = productAdminRouter
        }
        if(result[0].identity=='消息管理员'){
            menu = messageAdminRouter
        }
        if(result[0].identity=='用户'){
            menu = userRouter
        }
        res.send({
            status:0,
            data:menu,
        })
    })
}